package com.study.springcloudgateway.handler;

import com.alibaba.fastjson.JSONObject;
import com.study.api.Result;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.core.io.buffer.DataBufferUtils;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.stereotype.Component;
import reactor.core.publisher.Flux;

import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat;

/**
 * @description：条件过滤
 * @author：李军荣
 * @date: 2021/2/7 16:36
 */
@Component
public class SeckillConditionHandler {



    private static  SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    public Result conditonHanlder(String bodyStr){
        JSONObject parames = decodeBody(bodyStr);
        Integer activityId = (Integer) parames.get("activityId");
        Integer goodsId = (Integer) parames.get("goodsId");
        Integer userId = (Integer) parames.get("userId");
        String userActivition = (String) parames.get("userActivition");
        String orderPercentage = (String) parames.get("orderPercentage");


        return new Result.Builder<>().ok().bulider();
    }

    private JSONObject decodeBody(String body) {
        body = body.replaceAll("\r\n","");
        return JSONObject.parseObject(body);
    }


    private String resolveBodyFromRequest(ServerHttpRequest serverHttpRequest) {
        //获取请求体
        Flux<DataBuffer> body = serverHttpRequest.getBody();
        StringBuilder sb = new StringBuilder();
        body.subscribe(buffer -> {
            System.out.println("bodyString======="+buffer.readableByteCount());
            byte[] bytes = new byte[buffer.readableByteCount()];
            buffer.read(bytes);
            DataBufferUtils.release(buffer);
            String bodyString = new String(bytes, StandardCharsets.UTF_8);
            System.out.println("bodyString======="+bodyString);
            sb.append(bodyString);
        });
        return sb.toString();
    }



    private boolean validOrderPercentage(String orderPercentage) {
        return true;
    }

    private boolean validUserActivition(String userActivition) {
        return true;
    }

}
